
package xiaochangsama.ancientkungfugeneration;



import java.io.*;
import java.util.Base64;
import java.util.HashSet;
import java.util.Set;


/*
		// 创建PlayerQingGong对象
        PlayerQingGong player = new PlayerQingGong();

        // 添加整数
        boolean result1 = player.addQingGong(1); // 返回true
        boolean result2 = player.addQingGong(2); // 返回true
        boolean result3 = player.addQingGong(1); // 返回false，因为1已存在

        // 打印结果
        System.out.println("Result 1: " + result1);
        System.out.println("Result 2: " + result2);
        System.out.println("Result 3: " + result3);

        // 序列化为字符串
        String serialized = player.serializeToString();
        System.out.println("Serialized: " + serialized);

        // 反序列化字符串为PlayerQingGong对象
        PlayerQingGong deserialized = PlayerQingGong.deserializeFromString(serialized);

        // 验证反序列化后的对象
        if (deserialized != null) {
            System.out.println("Deserialized: " + deserialized.qingGongSet);
        }
 */
public class PlayerQingGong implements Serializable {
	private Set<Integer> qingGongSet;

	public PlayerQingGong() {
		this.qingGongSet = new HashSet<>();
	}
	public PlayerQingGong(Set<Integer> hashset){
		this.qingGongSet = hashset;
	}

	// 添加整数，如果整数已存在返回false，不存在返回true
	public boolean addQingGong(int qingGong) {
		return qingGongSet.add(qingGong);
	}

	// 序列化为字符串
	public String serializeToString() {
	return qingGongSet.toString();
	}

	// 反序列化字符串为PlayerQingGong对象
	public static PlayerQingGong deserializeFromString(String serialized) {
		Set<Integer> restoredSet = new HashSet<>();
		// 默认包含0
		restoredSet.add(0);

		// 如果字符串为 "[]"，直接返回空的HashSet
		if ("[]".equals(serialized)) {
			return new PlayerQingGong(restoredSet);
		}

		// 使用正则表达式匹配数字
		String[] elements = serialized.replaceAll("[{}]", "").split(", ");

		// 将分割的元素添加到HashSet中
		for (String element : elements) {
			// 使用正则表达式匹配数字，避免非数字字符引发异常
			if (element.matches("-?\\d+")) {
				restoredSet.add(Integer.parseInt(element));
			}
		}

		return new PlayerQingGong(restoredSet);
	}
}
